/**
 * 详情对话框
 */
var CourseInfoDlg = {
    data: {
        id: "",
        name: "",
        people: "",
        categoryId: "",
        content: "",
        intro: "",
        teacherName: "",
        createTime: "",
        updateTime: "",
        createUser: "",
        updateUser: "",
        cover: "",
        isFree: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
























    var umScore = UM.getEditor("content");
    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/course/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('courseForm', result.data);
    $("#coverimg").attr("src",  result.data.cover);

    var uploadInst=upload.render({
        elem: '#picBtn'
        , url: "/upload/upload"
        , before: function (obj) {
            layer.msg('文件上传中...', {
                icon: 16,
                shade: 0.01,
                time: 1000
            })
            setTimeout(function () {
                obj.preview(function (index, file, result) {
                    $('#coverimg').attr('src', result);
                });
            }, 2000)

        }
        , done: function (res) {
            if(res.stauts == 500) {
                layer.closeAll();
                Feng.error("上传失败！" + res.msg);
                //         return false;

            }else if(res.stauts==200) {
                setTimeout(function () {
                    $("#cover").val(res.url);
                    Feng.success(res.msg);
                }, 2000)
            }

        }
        , error: function () {
            Feng.error("上传图片失败！");
        }
    });






    $('#categoryName').click(function () {
        var formName = encodeURIComponent("parent.CourseInfoDlg.data.categoryName");
        var formId = encodeURIComponent("parent.CourseInfoDlg.data.categoryId");
        var treeUrl = encodeURIComponent("/category/tree");

        console.log(formName);
        console.log(formId);
        console.log(treeUrl);

        layer.open({
            type: 2,
            title: '上级分类',
            area: ['500px', '500px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#categoryId").val(CourseInfoDlg.data.categoryId);
                $("#categoryName").val(CourseInfoDlg.data.categoryName);
            }
        });
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/course/editItem", function (data) {
            Feng.success("更新成功！");
            window.location.href = Feng.ctxPath + '/course'
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/course'
    });
});